package cn.cstech.aijia.sns;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

import cn.cstech.aijia.sns.weixin.WeiXinController;
import cn.cstech.aijia.ui.base.utils.LocalUtils;
import cn.cstech.aijia.ui.base.utils.SPUtils;


public class SnsShareResultActivity extends Activity implements /*Response,*/ IWXAPIEventHandler {
//	IWeiboShareAPI mWeiboShareAPI;
	IWXAPI mIwxapi;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		mWeiboShareAPI = WeiBoController.getInstance(this, this)
//				.getIWeiboShareAPI();
		
		mIwxapi = WeiXinController.getInstance(this).getIwxapi();
		
//		if(mWeiboShareAPI != null){
//			mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
//		}
		
		if(mIwxapi != null){
			mIwxapi.handleIntent(getIntent(), this);
		}
	}

	/**
	 * @see {@link Activity#onNewIntent}
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		// 从当前应用唤起微博并进行分享后，返回到当前应用时，需要在此处调用该函数
		// 来接收微博客户端返回的数据；执行成功，返回 true，并调用
		// {@link IWeiboHandler.Response#onResponse}；失败返回 false，不调用上述回调
//		if(mWeiboShareAPI != null){
//			mWeiboShareAPI.handleWeiboResponse(intent, this);
//		}
		
		if(mIwxapi != null){
			mIwxapi.handleIntent(getIntent(), this);
		}
	}

	/*@Override
	public void onResponse(BaseResponse baseResponse) {
		 switch (baseResponse.errCode) {
	        case WBConstants.ErrorCode.ERR_OK:
	        	sendShareResultBroadcast(SnsErrorCode.SUCCESS.ordinal(),null);
	            break;
	        case WBConstants.ErrorCode.ERR_CANCEL:
	        	sendShareResultBroadcast(SnsErrorCode.CANCEL.ordinal(),null);
	            break;
	        case WBConstants.ErrorCode.ERR_FAIL:
	        	sendShareResultBroadcast(SnsErrorCode.FAILED.ordinal(),baseResponse.errMsg);
	            break;
	        }
		LocalUtils.showToast(this, baseResponse.errMsg);
		finish();
	}*/

	@Override
	public void onReq(BaseReq arg0) {
	}

	@Override
	public void onResp(BaseResp baseResponse) {
		switch (baseResponse.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			sendShareResultBroadcast(SnsErrorCode.SUCCESS.ordinal(), null);
//			LocalUtils.sendAppendAction(this);
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
		case BaseResp.ErrCode.ERR_COMM:
		case BaseResp.ErrCode.ERR_SENT_FAILED:
		case BaseResp.ErrCode.ERR_UNSUPPORT:
			sendShareResultBroadcast(SnsErrorCode.FAILED.ordinal(),baseResponse.errStr);
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			sendShareResultBroadcast(SnsErrorCode.SUCCESS.ordinal(), null);
//			LocalUtils.sendAppendAction(this);
			break;
		}
		LocalUtils.showToast(this, baseResponse.errStr);
		finish();
	}
	
	private void sendShareResultBroadcast(int errorCode,String errorMsg){
		Intent intent = new Intent(SPUtils.ACTION_SHARE_RESULT);
		intent.putExtra(SPUtils.EXTRA_CODE, errorCode);
		intent.putExtra(SPUtils.EXTRA_MESSAGE, errorMsg);
		sendBroadcast(intent);
	}
}
